package com.basic.library.base;


import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;

import com.basic.library.binding.command.BindingCommand;
import com.basic.library.binding.command.BindingCommandAbstract;
import com.basic.library.binding.viewadapter.recyclerview.SimpleViewModel;
import com.basic.library.bus.event.SingleLiveEvent;
import com.basic.library.httpmanager.task.IView;
import com.basic.library.httpmanager.task.LView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by goldze on 2017/6/15.
 */
public class BaseViewModel extends AndroidViewModel implements IBaseViewModel {
    private UIChangeLiveData uc;
    private LifecycleProvider<ActivityEvent> lifecycleActivity;
    private LifecycleProvider<FragmentEvent> lifecycleFragment;
    public IView iView;
    protected LView mLView;
    public SimpleViewModel simpleViewModel;
    protected Application application;

    public void postValue(int type, Object o) {
        Map<Integer, Object> m = new HashMap<>();
        m.put(type, o);
        getUC().getAllEvent().postValue(m);
    }

    public void call() {
        getUC().getAllEvent().call();
    }

    public BaseViewModel(@NonNull Application application) {
        super(application);
        this.application = application;
    }

    public void setIView(IView iView) {
        this.iView = iView;
    }

    public void setLView(LView lView) {
        this.mLView = lView;
    }

    /**
     * 注入RxLifecycle生命周期
     *
     * @param lifecycle
     */
    public void injectLifecycleActivityProvider(LifecycleProvider<ActivityEvent> lifecycle) {
        this.lifecycleActivity = lifecycle;
    }

    public LifecycleProvider<ActivityEvent> getLifecycleActivityProvider() {
        return lifecycleActivity;
    }

    public void injectLifecycleFragmentProvider(LifecycleProvider<FragmentEvent> lifecycle) {
        this.lifecycleFragment = lifecycle;
    }

    public LifecycleProvider<FragmentEvent> getLifecycleFragmentProvider() {
        return lifecycleFragment;
    }

    public UIChangeLiveData getUC() {
        if (uc == null) {
            uc = new UIChangeLiveData();
        }
        return uc;
    }

    public void showProgressDialog(String title) {
        uc.showDialogEvent.postValue(title);
    }

    public void dismissProgressDialog() {
        uc.dismissDialogEvent.call();
    }

    /**
     * 跳转页面
     *
     * @param clz 所跳转的目的Activity类
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    /**
     * 跳转页面
     *
     * @param clz    所跳转的目的Activity类
     * @param bundle 跳转所携带的信息
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Map<String, Object> params = new HashMap();
        params.put(ParameterField.CLASS, clz);
        if (bundle != null) {
            params.put(ParameterField.BUNDLE, bundle);
        }
        uc.startActivityEvent.postValue(params);
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     */
    public void startContainerActivity(String canonicalName, boolean statusBarTextColorBlack) {
        startContainerActivity(canonicalName, null, statusBarTextColorBlack);
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     * @param bundle        跳转所携带的信息
     */
    public void startContainerActivity(String canonicalName, Bundle bundle) {
        Map<String, Object> params = new HashMap();
        params.put(ParameterField.CANONICAL_NAME, canonicalName);
        if (bundle != null) {
            params.put(ParameterField.BUNDLE, bundle);
        }
        uc.startContainerActivityEvent.postValue(params);
    }

    /**
     * @param canonicalName
     * @param bundle
     * @param statusBarTextColorBlack 状态栏字体颜色是否为‘黑色’(默认白色字体)
     */
    public void startContainerActivity(String canonicalName, Bundle bundle, boolean statusBarTextColorBlack) {
        Map<String, Object> params = new HashMap();
        params.put(ParameterField.CANONICAL_NAME, canonicalName);
        if (statusBarTextColorBlack && bundle == null) bundle = new Bundle();
        if (bundle != null) {
            bundle.putBoolean(ContainerActivity.statusBarTextColorBlack, statusBarTextColorBlack);
            params.put(ParameterField.BUNDLE, bundle);
        }
        uc.startContainerActivityEvent.postValue(params);
    }

    /**
     * 关闭界面
     */
    public void finish() {
        uc.finishEvent.call();
    }

    /**
     * Disposable
     */
    public void onSubscribe(Disposable d) {
        uc.disposableEvent.postValue(d);
    }

    public BindingCommand<String> backClicked() {
        return new BindingCommand<>(this::finish);
    }

    /**
     * 返回上一层
     */
    public void onBackPressed() {
        uc.onBackPressedEvent.call();
    }

    @Override
    public void onAny(LifecycleOwner owner, Lifecycle.Event event) {
    }

    @Override
    public void onCreate() {
    }

    /**
     * fragment
     */
    public void onViewCreated() {

    }

    @Override
    public void onDestroy() {
    }

    @Override
    public void onStart() {
    }

    @Override
    public void onStop() {
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onFinish() {
    }

    /**
     * fragment第一次可见
     */
    @Override
    public void bindingOver() {

    }

    //标题的绑定
    public ObservableField<String> title = new ObservableField<>("");
    public ObservableField<String> title_right = new ObservableField<>("");
    public ObservableField<Integer> drawable_right = new ObservableField<>(0);
    public ObservableField<Integer> drawable_rightTint = new ObservableField<>(0);
    public ObservableField<Integer> drawable_rightTextColor = new ObservableField<>(0);
    public ObservableField<Integer> drawable_end = new ObservableField<>(0);
    public ObservableField<Integer> drawable_endTint = new ObservableField<>(0);
    public ObservableField<BindingCommand> onclick_right = new ObservableField<>();
    public ObservableField<BindingCommand> onclick_back = new ObservableField<>();
    public ObservableField<BindingCommand> onclick_end = new ObservableField<>();

    public final class UIChangeLiveData extends SingleLiveEvent {

        private SingleLiveEvent<String> showDialogEvent;
        private SingleLiveEvent<Void> dismissDialogEvent;
        private SingleLiveEvent<Map<String, Object>> startActivityEvent;
        private SingleLiveEvent<Map<String, Object>> startContainerActivityEvent;
        private SingleLiveEvent<Void> finishEvent;
        private SingleLiveEvent<Void> onBackPressedEvent;
        private SingleLiveEvent<Disposable> disposableEvent;
        private SingleLiveEvent<Map<Integer, Object>> allEvent = new SingleLiveEvent<>();

        public SingleLiveEvent<String> getShowDialogEvent() {
            return showDialogEvent = createLiveData(showDialogEvent);
        }

        public SingleLiveEvent<Void> getDismissDialogEvent() {
            return dismissDialogEvent = createLiveData(dismissDialogEvent);
        }

        public SingleLiveEvent<Map<String, Object>> getStartActivityEvent() {
            return startActivityEvent = createLiveData(startActivityEvent);
        }

        public SingleLiveEvent<Map<String, Object>> getStartContainerActivityEvent() {
            return startContainerActivityEvent = createLiveData(startContainerActivityEvent);
        }

        public SingleLiveEvent<Void> getFinishEvent() {
            return finishEvent = createLiveData(finishEvent);
        }

        public SingleLiveEvent<Void> getOnBackPressedEvent() {
            return onBackPressedEvent = createLiveData(onBackPressedEvent);
        }

        public SingleLiveEvent<Disposable> getDisposableEvent() {
            return disposableEvent = createLiveData(disposableEvent);
        }

        public SingleLiveEvent<Map<Integer, Object>> getAllEvent() {
            return allEvent = createLiveData(allEvent);
        }

        private SingleLiveEvent createLiveData(SingleLiveEvent liveData) {
            if (liveData == null) {
                liveData = new SingleLiveEvent();
            }
            return liveData;
        }

        @Override
        public void observe(LifecycleOwner owner, Observer observer) {
            super.observe(owner, observer);
        }
    }

    public static final class ParameterField {
        public static String CLASS = "CLASS";
        public static String CANONICAL_NAME = "CANONICAL_NAME";
        public static String BUNDLE = "BUNDLE";
    }

    /**
     * 点击事件
     * 无参返回用BindingAction，返回id的用BindingConsumer
     */
    public BindingCommand<Integer> mOnClick = new BindingCommand<Integer>(this::mOnClick);

    public void mOnClick(int vId) {

    }

    /**
     * TextView同样的id，不同的text值(如多个include，必须保证text值不一样)
     */
    public BindingCommandAbstract<String> mOnClickText = new BindingCommandAbstract<String>(this::mOnClickText) {
    };

    public void mOnClickText(String text) {

    }
}
